package com.xhs.main.controller.xsb;

import com.alibaba.fastjson.JSONObject;
import com.xhs.main.entity.result.LayuiResponseResult;
import com.xhs.main.entity.xsb.Client;
import com.xhs.main.entity.xsb.Plan;
import com.xhs.main.service.xsb.impl.XsbPlanServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hjr
 * @create 2020-10-20-下午 5:44
 */

@RestController
public class PlanController {

    @Autowired
    XsbPlanServiceImpl planService;


    @RequestMapping("/addPlan")
    public void addPlan( Plan plan){
        plan.setXsbDApprove("制单");
        plan.setXsbDStatus("审批中");
        System.out.println(plan);
        planService.addPlan(plan);

    }

    @RequestMapping("/getAllPlan")
    public LayuiResponseResult showAllPlan(){
        List<Plan> allPlan = planService.getAllPlan();

        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(allPlan.size());

        result.setData(allPlan);
//        Map<String,Object> map = new HashMap<>();
////                "code": 0,
////                "msg": "",
////                "count": 1000,
////                "data": [
//        map.put("code",0);
//        map.put("msg","成功");
//        map.put("count",allPlan.size());
//        map.put("data",allPlan);
        return  result;
    }


    @RequestMapping("/getPlan")
    public LayuiResponseResult getPlan(@RequestParam("searchParams") String searchParams){


        Plan plan = JSONObject.parseObject(searchParams, Plan.class);

        JSONObject jsonObject = JSONObject.parseObject(searchParams);
        String xsbCName = (String) jsonObject.get("xsbCName");
        Client client =  new Client();
        client.setXsbCName(xsbCName);
        plan.setClient(client);


        List<Plan> byVague = planService.getPlanByVague(plan);
        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(byVague.size());

        result.setData(byVague);

        return result;
    }


    @RequestMapping("/updatePlan")
    public LayuiResponseResult updatePlan(@RequestParam("searchParams") String searchParams){

        Plan plan = JSONObject.parseObject(searchParams, Plan.class);

        List<Plan> plans = planService.updatePlan(plan);

        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(plans.size());

        result.setData(plans);
        return result;
    }
}
